// miniprogram/pages/admin/client/client.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    skip: 0,
    latest: 1,
    items: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.loadItems();
  },

  loadItems: function (e) {
    const that = this;
    app.login(user => {
      if (!user.studioid) {
        wx.navigateBack();
        return;
      }
      wx.showNavigationBarLoading();
      wx.cloud.callFunction({
        name: 'crud',
        data: {
          op: 'get',
          table: 'customer',
          skip: that.data.skip,
          where: {
            studioid: user.studioid
          }
          // orderBy: [{'updatetime': desc}]
        },
        success: res => {
          console.log(res)
          let latest = res.result.data.length,
            items = that.data.items
          that.data.latest = latest
          items = items.concat(res.result.data)
          that.setData({
            items
          })
          wx.hideNavigationBarLoading();
          //停止下拉刷新
          wx.stopPullDownRefresh();
        }
      })
    })
  },

  showActions(e) {
    const that = this;
    const index = e.currentTarget.dataset.index; // TODO
    const actionindex = parseInt(e.currentTarget.dataset.actionindex);
    const item = this.data.items[index];
    switch (actionindex) {
      case 0:
        const phone = item.phone;
        if (phone) {
          wx.makePhoneCall({
            phoneNumber: phone
          }).then(res => {
            console.log(res);
          }).catch(e => {})
        }
        break;
      case 1:
        // 跳转修改
        wx.navigateTo({
          url: `newCustomer/newCustomer?_id=${item._id}`,
        })
        break;
      case 2:
        wx.navigateTo({
          url: `detail/detail?_id=${item._id}`,
        })
        break;
      case 3:
        wx.setClipboardData({
          data: item.wechat,
          success(res) {
            //  wx.hideToast(); //隐藏复制成功的弹窗提示,根据需求可选
          }
        })

        break;
      case 4:
        // TODO 查看
        that.removeItem(item)
        break;
    }
    // wx.showActionSheet({
    //   itemList: ['呼叫','修改','详情','删除'],
    //   success: res => {
    //     switch (res.tapIndex) {
    //       case 0:
    //         wx.makePhoneCall({
    //           phoneNumber: item.phone
    //         }).then(res=>{
    //           console.log(res);
    //         }).catch(e=>{
    //         })
    //         break;
    //       case 1:
    //         // 跳转修改
    //         wx.navigateTo({
    //           url: `newCustomer/newCustomer?_id=${item._id}`,
    //         })
    //         break;
    //       case 2:
    //           wx.navigateTo({
    //             url: `detail/detail?_id=${item._id}`,
    //           })
    //           break;
    //       case 3:
    //           that.removeItem(item)
    //           break;
    //       case 4:
    //         // TODO 查看
    //         wx.navigateTo({
    //           url: `../order/order?customerid=${item._id}`,
    //         })
    //         break;
    //     }

    //   },
    //   fail: e => {

    //   }
    // })
  },


  searchCustomer: function (e) {
    const form = e.detail.value;
    const that = this;
    if (!form.name) {
      this.loadItems();
      return;
    }
    wx.showNavigationBarLoading();
    wx.cloud.callFunction({
      name: 'customer',
      data: {
        op: 'search',
        studioid: app.globalData.user.studioid,
        name: form.name
        // orderBy: [{'updatetime': desc}]
      },
      success: res => {
        console.log(res.result);
        that.setData({
          items: res.result.list
        })
        wx.hideNavigationBarLoading();
        //停止下拉刷新
        wx.stopPullDownRefresh();
      }
    })
  },

  removeItem: function (item) {
    const that = this;
    wx.showModal({
      title: '确认',
      content: '您确认要删除此客户信息？',
      success(res) {
        if (res.confirm) {
          // TODO 在COS上删除作品对应的图片
          wx.cloud.callFunction({
            name: 'crud',
            data: {
              op: 'remove',
              table: 'customer',
              _id: item._id
            },
            success: res => {
              if (res.result.stats.removed == 1) {
                that.onLoad()
              }
            }
          })
        }
      }
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.latest > 0) {
      this.data.skip += 20
      this.loadItems({})
    } else {
      wx.showModal({
        content: '没有更多内容了',
        showCancel: false,
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})